package com.sky.service.impl;


import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

   @Autowired
   private DishMapper dishMapper;

   @Autowired
   private ShoppingCartMapper shoppingCartMapper;

   @Autowired
   private SetmealMapper setmealMapper;

    @Override
    public void addCart(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);

        //判断商品是否在购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart =shoppingCartMapper.selectBy(shoppingCart);
        if (cart ==null){  //购物车没有该商品数据
            //判断新增套餐还是菜品
            if (dto.getDishId() !=null){
                Dish dish=dishMapper.selectById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());

        }else { //套餐
                Setmeal setmeal=setmealMapper.selectById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);
        }else { //代表购物车有数据
             cart.setNumber(cart.getNumber()+1);
             shoppingCartMapper.update(cart);
        }
    }

    @Override
    public List<ShoppingCart> list() {

        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    @Override
    public void clean() {
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }

    @Override
    public void delete(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);

        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.listByd(shoppingCart);
        if(list != null && list.size() > 0) {
            shoppingCart = list.get(0);

            Integer number = shoppingCart.getNumber();
            if (number == 1) {
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteByid(shoppingCart.getId());
            } else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartMapper.update(shoppingCart);
            }

        }
    }
}
